import httpUtils from "./XmlHttpTool";
import taskScrollview from "./taskScrollview";
import allVariableJs from "./allVariable";
cc.Class({
    extends: cc.Component,

    properties: {
        headSprite: cc.Node,
        nameLabel: cc.Label,
        descLabel: cc.Label,
        sureBtn: cc.Button,
        noprogress: cc.Node,
        alreadtprogress: cc.Node,
        progressLabel: cc.Label,
        alreadyGet: cc.Node,
        starttaskBtn: cc.Button,
        alreadygetSign: cc.Node,
        taskdoingBtn: cc.Button,
        backGrondImage: cc.Node,
        taskDic: "",
        taskScrollViewJs: taskScrollview,
        showHubProjess: cc.Node,
        SvprogressPrefab: cc.Prefab




    },

    startTaskClick: function () {
        console.log("去做任务");
        var _this = this
        if (allVariableJs.allvideoAD) {

            allVariableJs.allvideoAD.offClose()
            allVariableJs.allvideoAD.offError()
            allVariableJs.allvideoAD.onClose(res => {
                if (res && res.isEnded || res === undefined) {
                    var that = this

                    httpUtils.NhttpPost("/house/task_finish", { "task_id": that.taskDic["id"] }, function (res) {

                        var mainNode = cc.find("Canvas/MainCamera/taskAllView/taskScrollview/view/content")
                        mainNode.getComponent("taskScrollview").init();
                        var startgameNode = cc.find("Canvas/MainCamera/StartGameView")
                        startgameNode.getComponent("StartGameView").getUserInfoMessage()
                        startgameNode.getComponent("StartGameView").notifyPrize()

                    })


                } else {
                    // 播放中途退出，不下发游戏奖励
                    console.log("播放中途退出，不下发游戏奖d励", res)
                }
            })

            allVariableJs.allvideoAD.onError((res) => {

                let errcode = res["errCode"]
                _this.tishifinde()
                if (errcode == 1004) {
                    console.log("无广告资源")
                } else {
                    console.log("无广告资源")
                }
            })

            allVariableJs.allvideoAD.load((res) => {
                console.log('激励视频 广告加载成功', res)

            })
                .then(() => allVariableJs.allvideoAD.show())
                .catch(function (err) {
                    console.log('激励视频抓取结果', err)
                    _this.tishifinde()
                })

        } else {

            var _this = this
            let rewardedVideoAd = wx.createRewardedVideoAd({

                adUnitId: 'adunit-8536f460f8a4925c'

            })
            allVariableJs.allvideoAD = rewardedVideoAd
            allVariableJs.allvideoAD.offClose()
            allVariableJs.allvideoAD.onClose(res => {
                if (res && res.isEnded || res === undefined) {
                    var that = this

                    httpUtils.NhttpPost("/house/task_finish", { "task_id": that.taskDic["id"] }, function (res) {

                        var mainNode = cc.find("Canvas/MainCamera/taskAllView/taskScrollview/view/content")
                        mainNode.getComponent("taskScrollview").init();
                        var startgameNode = cc.find("Canvas/MainCamera/StartGameView")
                        startgameNode.getComponent("StartGameView").getUserInfoMessage()
                        startgameNode.getComponent("StartGameView").notifyPrize()

                    })

                } else {
                    // 播放中途退出，不下发游戏奖励
                    console.log("播放中途退出，不下发游戏奖d励", res)
                }
            })

            allVariableJs.allvideoAD.onError((res) => {

                let errcode = res["errCode"]
                _this.tishifinde()
                if (errcode == 1004) {

                    console.log("无广告资源")
                } else {
                    console.log("无广告资源")

                }
            })
            allVariableJs.allvideoAD.load((res) => {
                console.log('激励视频 广告加载成功', res)

            })
                .then(() => allVariableJs.allvideoAD.show())
                .catch(function (err) {
                    console.log('激励视频抓取结果', err)
                    _this.tishifinde()
                })
        }

    },



    // onLoad () {},


    gotoTaskClick: function () {

        console.log("领取奖励");

        var that = this
        httpUtils.NhttpPost("/marmot/task_accept", { "task_id": that.taskDic["id"] }, function (res) {

            var mainNode = cc.find("Canvas/MainCamera/taskAllView/taskScrollview/view/content")
            mainNode.getComponent("taskScrollview").init();


        })

    },

    tishifinde: function () {

        if (cc.isValid(this.showHubProjess)) {
            this.showHubProjess.destroy();
        }
        this.showHubProjess = cc.instantiate(this.SvprogressPrefab);
        var taskAllViewNode = cc.find("Canvas/MainCamera/taskAllView")
        taskAllViewNode.addChild(this.showHubProjess);
        this.showHubProjess.getComponent('SVProgressView').init("今天的广告已经看完了哦！ 奖励无法领取哦。请明天再来！")
        let finish = cc.callFunc(() => {

            this.showHubProjess.destroy();

        }, this);
        let action = cc.sequence(cc.fadeIn(0.6), cc.delayTime(3), cc.fadeOut(1), finish);
        this.showHubProjess.runAction(action);
    },


    start() {

    },


    init(bageDic) {

        console.log("传过来的任务信息", bageDic)
        this.taskDic = bageDic
        var that = this
        cc.loader.load({ url: bageDic["thumbnail"], type: 'png' }, function (err, img) {

            var mylogo = new cc.SpriteFrame(img);

            that.headSprite.getComponent(cc.Sprite).spriteFrame = mylogo;

            that.headSprite.width = 106

            that.headSprite.height = 106
        });
        this.nameLabel.getComponent(cc.Label).string = bageDic["title"]
        this.descLabel.getComponent(cc.Label).string = bageDic["desc"]

        if (bageDic["button_status"] == 1) {
            console.log("任务未领取")
            this.starttaskBtn.node.active = true


        } else if (bageDic["button_status"] == 2) {

            this.noprogress.active = true
            this.alreadtprogress.active = true
            this.taskdoingBtn.node.active = true
            this.progressLabel.node.active = true
            this.progressLabel.getComponent(cc.Label).string = bageDic["finish_num"] + '/' + bageDic["condition"]
            this.alreadtprogress.active = true
            this.alreadtprogress.width = (bageDic["finish_num"] / bageDic["condition"]) * 116
            this.alreadtprogress.x = -(116 - (bageDic["finish_num"] / bageDic["condition"]) * 116) / 2
        } else if (bageDic["button_status"] == 3) {

            this.sureBtn.node.active = true
            this.alreadygetSign.active = true
            this.backGrondImage.active = true
            var colorCode = new cc.Color(255, 255, 255, 255);
            this.nameLabel.node.color = colorCode
            this.nameLabel.getComponent(cc.LabelOutline).color = colorCode
            this.descLabel.node.color = colorCode
            this.descLabel.getComponent(cc.LabelOutline).color = colorCode


        } else {
            this.alreadyGet.active = true
        }

    }
});
